package com.ybdz.scanner.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.ybdz.scanner.R;
import com.ybdz.scanner.api.ApiStore;
import com.ybdz.scanner.base.BaseActivity;
import com.ybdz.scanner.bean.chuKuBean;
import com.ybdz.scanner.bean.TakeCommitBean;
import com.ybdz.scanner.bean.TakeInventoryBean;
import com.ybdz.scanner.utils.ListUtil;
import com.ybdz.scanner.utils.MyToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @ClassName: ChuKuSimpleActivity
 * @author: zhao
 * @Description: 正常出库的界面
 * @date 2018/2/24 17:28
 */
public class ChuKuSimpleActivity extends BaseActivity {

    @BindView(R.id.ll_back)
    LinearLayout mLlBack;
    @BindView(R.id.home)
    FrameLayout mHome;
    @BindView(R.id.name)
    TextView mName;
    @BindView(R.id.place)
    TextView mPlace;
    @BindView(R.id.breed)
    TextView mBreed;
    @BindView(R.id.rank)
    TextView mRank;
    @BindView(R.id.batch)
    TextView mBatch;
    @BindView(R.id.birth_date)
    TextView mBirthDate;
    @BindView(R.id.specification)
    TextView mSpecification;
    @BindView(R.id.weight)
    TextView mWeight;
    @BindView(R.id.conformity)
    TextView mConformity;
    @BindView(R.id.bt_save)
    Button mBtSave;
    @BindView(R.id.bt_back)
    Button mBtBack;
    @BindView(R.id.empty)
    QMUIEmptyView mEmpty;


    private TakeInventoryBean.DataBean data;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_normal);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        mEmpty.show(true);
        if (data != null) {
            initData();
            mEmpty.hide();
        } else {
            mEmpty.show(false, "未查询到该单品信息", null, null, null);
        }
    }

    /**
     * EventBus传递的数据
     *
     * @param data
     */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getData(TakeInventoryBean.DataBean data) {
        this.data = data;
    }

    @SuppressLint("SetTextI18n")
    private void initData() {
        if (data != null){
            String productName = data.getProductName();
            if (productName == null || productName.equalsIgnoreCase("null")){   //双引号必须保留！
                mName.setText("");
            }else {
                mName.setText(String.valueOf( productName));
            }

            String origin = data.getOrigin();
            if (origin == null || origin.equalsIgnoreCase("null")){
                mPlace.setText("原产地：");
            }else {
                mPlace.setText(String.valueOf("原产地：" + origin));
            }

            String varieties = data.getVarieties();
            if (varieties == null || varieties.equalsIgnoreCase("null")){
                mBreed.setText("品种：");
            }else {
                mBreed.setText(String.valueOf("品种：" + varieties));
            }

            String productBatch = data.getProductBatch();
            if (productBatch == null || productBatch.equalsIgnoreCase("null")){
                mBatch.setText("产品批次：");
            }else {
                mBatch.setText(String.valueOf("产品批次：" + productBatch));
            }

            String grade = data.getGrade();
            if (grade == null || grade.equalsIgnoreCase("null")){
                mRank.setText("等级：");
            }else {
                mRank.setText(String.valueOf("等级：" + grade));
            }

            String producedDate = data.getProducedDate();
            if (producedDate == null || producedDate.equalsIgnoreCase("null")){
                mBirthDate.setText("生产日期：");
            }else {
                mBirthDate.setText(String.valueOf("生产日期：" + producedDate));
            }

            String specification = data.getSpecification();
            if (specification == null || specification.equalsIgnoreCase("null")){
                mSpecification.setText("规格：");
            }else {
                mSpecification.setText(String.valueOf("规格：" + specification));
            }

            double weight = Double.parseDouble(data.getActualWeight() + "") / 1000;
            mWeight.setText("实际重量：" + weight + " Kg");
            boolean isQualified = data.isIsQualified();
            if (isQualified) {
                mConformity.setText("是否合格：合格");
            } else {
                mConformity.setText("是否合格：不合格");
            }
        }
    }

    @OnClick({R.id.ll_back, R.id.home, R.id.bt_save, R.id.bt_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:                  //返回
                finish();
                break;
            case R.id.home:                     //返回首页
                startActivity(new Intent(ChuKuSimpleActivity.this, MainActivity.class));
                finish();
                break;
            case R.id.bt_save:                  //出库请求服务器
                saveOut();
                break;
            case R.id.bt_back:                  //返回
                finish();
                break;
        }
    }

    /**
     * 出库单品的网络请求
     */
    private void saveOut() {
        if (data != null){
            double weight = Double.parseDouble(data.getActualWeight() + "") / 1000;
            if (weight <= 0){
                MyToast.show(this,"该单品重量为"+weight+"不允许出库");
                return;
            }
        }

        showLoading(ChuKuSimpleActivity.this);
        ApiStore.saveOut(String.valueOf(data.getId()), String.valueOf(data.getSkuId()),String.valueOf( data.getFormId()), data.getProductBatch())
                .enqueue(new Callback<TakeCommitBean>() {
                    @Override
                    public void onResponse(Call<TakeCommitBean> call, Response<TakeCommitBean> response) {
                        hideLoading();
                        if (response.code() == 200 && response.isSuccessful()){
                            TakeCommitBean body = response.body();
                            if (body != null){
                                String status = body.getStatus();
                                if (status.equals("200")){
                                    String productName = data.getProductName();
                                    boolean isQualified = data.isIsQualified();
                                    chuKuBean historyBean = new chuKuBean(String.valueOf(productName==null?"":productName),String.valueOf(data.getActualWeight()/1000+"kg"),String.valueOf(isQualified ?"合格":"不合格"));
                                    ListUtil.addChuKuList(historyBean);

                                    MyToast.show(ChuKuSimpleActivity.this, "出库成功");

                                    ChuKuSimpleActivity.this.finish();
                                }else {
                                    onFail(ChuKuSimpleActivity.this,String.valueOf(body.getMsg()));
                                }
                            }
                        }else {
                            onFail(ChuKuSimpleActivity.this,"服务器繁忙");
                        }

                    }

                    @Override
                    public void onFailure(Call<TakeCommitBean> call, Throwable t) {
                        hideLoading();
                        onFailTimeOut(ChuKuSimpleActivity.this);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}
